package com.web.utils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @desc AES 加密工具类
 */
public class AESUtil {

	static Logger logger = LoggerFactory.getLogger(AESUtil.class);
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法

    /**
     * AES 加密操作
     *
     * @param content 待加密内容
     * @param password 加密密码
     * @return 返回Base64转码后的加密数据
     */
    public static String encrypt(String content, String password) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器

            byte[] byteContent = content.getBytes("GBK");

            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));// 初始化为加密模式的密码器

            byte[] result = cipher.doFinal(byteContent);// 加密

            return Base64.encodeBase64String(result);//通过Base64转码返回
        } catch (Exception e) {
            logger.info(AESUtil.class.getName()+"加密失败:"+e.getMessage(), e);
        }

        return null;
    }

    /**
     * AES 解密操作
     *
     * @param content
     * @param password
     * @return
     */
    public static String decrypt(String content, String password) {

        try {
            //实例化
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

            //使用密钥初始化，设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));

            //执行操作
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));

            return new String(result, "GBK");
        } catch (Exception e) {
            logger.info(AESUtil.class.getName()+"解密失败:"+e.getMessage(), e);
        }

        return null;
    }

    /**
     * 生成加密秘钥
     *
     * @return
     */
    private static SecretKeySpec getSecretKey(final String password) {
        //返回生成指定算法密钥生成器的 KeyGenerator 对象
    	  return new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);// 转换为AES专用密钥
    }

    public static void main(String[] args) {

    	String pwd = "hellohellohelloa";
		try {
//			List<String> wdatas= new ArrayList<String>();
//			List<String> rows= FileUtils.readLines(new File("F:/实名制资料/银行接口/银行实名制平台对接协议及示例v0.6.3/AAAAAAAAA-01-20180101-FEAD1234HJKL8769-未加密.txt"), "GBK");
//			for(String row:rows) {
//				//System.out.println(row);
//				wdatas.add(AESUtil.encrypt(row, pwd)+"\n");
//			}
//			FileUtils.writeLines(new File("F:/实名制资料/银行接口/银行实名制平台对接协议及示例v0.6.3/AAAAAAAAA-01-20180101-FEAD1234HJKL8769-加密.txt"), wdatas, "\n");
//
//			//解密
//			rows= FileUtils.readLines(new File("F:/实名制资料/银行接口/银行实名制平台对接协议及示例v0.6.3/AAAAAAAAA-01-20180101-FEAD1234HJKL8769-加密.txt"), "GBK");
//			for(String row:rows) {
//				System.out.println(row);
//				System.out.println(AESUtil.decrypt(row, pwd));

//			}
            List<String> rows1= Stream.of("QxZu7qxj+mVVHTLQ86z6EQ==", "6ovobD1sQyOGXE8lI2bj/3c0qXgtNpi1l0nJaey8KZKaAe7Nc0bQfzT7MJEz4uxmsa3bFW1WOAm45p/PcrcRVTfMRgaEbSZlWa0olBVrLPPykuLQ6L4zULFDhDt0gf4aqqHyb4mcEyMhjgrPh7LC4hmPBAevrEVqxmdRuztbjfLKtVSE5T2xTzlw8hVHze9hFMQyDKY1jhIXR71f6N1AUiI6HhMxHkqZAZ0II9QI95rpy7eJeLbgD4Vl3VMJV/m/+dOXMLG4os8nQrpqwKKQKHDjMByVbee3QXDldKOHKBlO2jPyNj2524xlji3KRByB", "td0swEdrrZ0ks00fLa+/FnKM6xf88u+RoNEtO0HjbI2aAe7Nc0bQfzT7MJEz4uxmsa3bFW1WOAm45p/PcrcRVTfMRgaEbSZlWa0olBVrLPPykuLQ6L4zULFDhDt0gf4aqqHyb4mcEyMhjgrPh7LC4hmPBAevrEVqxmdRuztbjfJOqH1UPSx+kQNykRuYBoTU4BqGtCH8pNxa3sFAxuvDUEilKUqMZjeZ/0ExTrnym5eWjAY0MEwq+Vp+oF77+SI7iYVy8t6ab1TNx9zVaMs75w3wlPHRoAJhPdqiTUPo4mg=").collect(Collectors.toList());
            List<String> rows2= Arrays.asList("QxZu7qxj+mVVHTLQ86z6EQ==","6ovobD1sQyOGXE8lI2bj/3c0qXgtNpi1l0nJaey8KZKaAe7Nc0bQfzT7MJEz4uxmsa3bFW1WOAm45p/PcrcRVTfMRgaEbSZlWa0olBVrLPPykuLQ6L4zULFDhDt0gf4aqqHyb4mcEyMhjgrPh7LC4hmPBAevrEVqxmdRuztbjfLKtVSE5T2xTzlw8hVHze9hFMQyDKY1jhIXR71f6N1AUiI6HhMxHkqZAZ0II9QI95rpy7eJeLbgD4Vl3VMJV/m/+dOXMLG4os8nQrpqwKKQKHDjMByVbee3QXDldKOHKBlO2jPyNj2524xlji3KRByB","td0swEdrrZ0ks00fLa+/FnKM6xf88u+RoNEtO0HjbI2aAe7Nc0bQfzT7MJEz4uxmsa3bFW1WOAm45p/PcrcRVTfMRgaEbSZlWa0olBVrLPPykuLQ6L4zULFDhDt0gf4aqqHyb4mcEyMhjgrPh7LC4hmPBAevrEVqxmdRuztbjfJOqH1UPSx+kQNykRuYBoTU4BqGtCH8pNxa3sFAxuvDUEilKUqMZjeZ/0ExTrnym5eWjAY0MEwq+Vp+oF77+SI7iYVy8t6ab1TNx9zVaMs75w3wlPHRoAJhPdqiTUPo4mg=");
            for(String row:rows1) {
				System.out.println(AESUtil.decrypt(row, pwd));
			}


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



    }

}
